package controllers;


import models.Author;
import models.Publication;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;

import configs.Config;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class PublicationController {

    private final ObjectContainer db;
    private final Config conn;
    public PublicationController(Config conn) {
        this.conn = conn;
        this.db = this.conn.getConn();
    }

    public void show(JTable tablesach, String txttim) {
        DefaultTableModel model = (DefaultTableModel) tablesach.getModel();
        if (txttim != null || !"".equals(txttim)) {
            ObjectSet<Publication> pubs = this.db.queryByExample(Publication.class);
            int stt = 0;
            for (Publication pub : pubs) {
                Object[] row = {++stt, pub.getTitle(), pub.getYear()};
                model.addRow(row);
            }
        } else {
            Publication search = new Publication(txttim); 
            ObjectSet<Publication> pubs = this.db.queryByExample(search); 
            int stt = 0;
            for (Publication pub : pubs) {
                Object[] row = {++stt, pub.getTitle(), pub.getYear()};
                model.addRow(row);
            }
        }
    }
        
    public Publication get(String title)
    {
          Publication pub = (Publication) this.db.queryByExample(new Publication(title)).next();
          return pub;
    }
    
    public void search(JTable tablesach, String txttim)
    {
        DefaultTableModel model = (DefaultTableModel) tablesach.getModel();
            Publication search = new Publication(txttim); 
            ObjectSet<Publication> pubs = this.db.queryByExample(search); 
            int stt = 0;
            for (Publication pub : pubs) {
                Object[] row = {++stt, pub.getTitle(), pub.getYear()};
                model.addRow(row);
            }
    }
    
    public void add(String title, int year, Object[][] authors) throws ParseException {
        Publication publication = new Publication(title, year);
        int Rows = authors.length;
        for (int i = 0; i < Rows; i++) {
            Author auth = new Author(authors[i][1].toString());
            SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
            Date birthday;
            try {
                birthday = sdf.parse(authors[i][2].toString());
            } catch (Exception ex) {
                birthday = sdf.parse(sdf.format(new Date()));
            }
            auth.setBirthday(birthday);
            auth.addPub(publication);
            publication.addAuthor(auth);
        }
        this.db.store(publication);
    }

    public void edit(JTable tabletacgia, List<Author> authors) throws ParseException {
        DefaultTableModel model = (DefaultTableModel) tabletacgia.getModel();
        int stt = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
        for (Author author : authors) {
            Object[] row = {++stt, author.getName(), sdf.format(author.getBirthday())};
            model.addRow(row);
        }
    }
    
    public void store(Publication pub, String title, int year, Object[][] authors) throws ParseException
    {
        pub.setTitle(title);
        pub.setYear(year);
        List<Author> newauthors = new ArrayList<Author>();
        int numRow = authors.length;
        for(int i = 0; i < numRow; i++)
        {
            Author auth = new Author(authors[i][1].toString());
             SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy");
            Date birthday;
            try {
                birthday = sdf.parse(authors[i][2].toString());
            } catch (Exception ex) {
                birthday = sdf.parse(sdf.format(new Date()));
            }
            auth.setBirthday(birthday);
            auth.addPub(pub);
            newauthors.add(auth);
        }
            pub.setAuthors(newauthors);
            this.db.store(pub);
    }

    public void delete(String title) {
        Publication pubdel = (Publication) this.db.queryByExample(new Publication(title)).next();
        this.db.delete(pubdel);
    }

    public void clearAll() {
        ObjectSet<Publication> pubs = this.db.queryByExample(Publication.class);
        while (pubs.hasNext()) {
            this.db.delete(pubs.next());
        }
    }

    public void close() {
        if (this.db.close()) {
            System.out.println("Da dong ket noi CSDL!");
        }
    }
    
    public Config getConn()
    {
        return this.conn;
    }
}
